//
//  ShareHomeViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/16.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "ShareHomeViewController.h"
#import "Macros.h"
#import "SDTableViewCell.h"
#import "SDRecordViewController.h"
#import "SDDetailViewController.h"
#import "OrderFormViewController.h"

@interface ShareHomeViewController ()
{
    CGFloat height;
    NSInteger page;
}

@property (nonatomic,strong)NSMutableArray *sdArray;

@end

@implementation ShareHomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColors(239, 239, 244, 1);
    self.navigationItem.title = @"晒单";
    page = 1;
    [self createRightBtn];
    [self createTableView];
    [self requestData];
    
}
- (NSMutableArray *)sdArray
{
    if (!_sdArray) {
        _sdArray = [NSMutableArray array];
    }
    return _sdArray;
}


- (void)requestData
{
    NSString *sdHomeUrl = [NSString stringWithFormat:@"%@%@",kBaseUrl,kSDHomeUrl];
    
    [NetworkHandle getUpDataWithUrl:sdHomeUrl class:NSClassFromString(@"SDModel") dictionary:@{@"page":[NSNumber numberWithInteger:page]} firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.sdArray removeAllObjects];
        }
        [self.sdArray addObjectsFromArray:array];
        if (self.sdArray.count >= 10 && self.shareHomeTableView.footer == nil) {
            self.shareHomeTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        [self.shareHomeTableView.header  endRefreshing];
        [self.shareHomeTableView.footer endRefreshing];
        [self.shareHomeTableView reloadData];
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
}
- (void)createRightBtn
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
//    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn1 setBackgroundImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
//    btn1.frame = CGRectMake(44,0, 44, 44);
//    [btn1 addTarget:self action:@selector(btn1Action:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *btn2= [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"sd01"] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btn2Action:) forControlEvents:UIControlEventTouchUpInside];
    btn2.frame = CGRectMake(0, 0, 44, 44);
   // [view addSubview:btn1];
    [view addSubview:btn2];
    
}

//跳转晒单界面
- (void)btn2Action:(UIButton *)button
{
    OrderFormViewController *VC = [[OrderFormViewController alloc] init];
    VC.pushType = 3;  // 待晒单模块
     self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:VC animated:NO];
}
- (void)createTableView
{
   self.shareHomeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.shareHomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.shareHomeTableView.delegate = self;
    self.shareHomeTableView.dataSource =self;
    self.shareHomeTableView.backgroundColor = [UIColor clearColor];
    self.shareHomeTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
        [self.view addSubview:self.shareHomeTableView];
}

- (void)headerRereshing
{
    page = 1;
    
    [self requestData];
}

- (void)footerRefresh
{
    page++;
    [self requestData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.sdArray.count>0) {
        return self.sdArray.count;
    }
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

      SDTableViewCell *cell;cell = [tableView dequeueReusableCellWithIdentifier:@"sdCell"];
        
    if (!cell) {
    
    cell = [[[NSBundle mainBundle] loadNibNamed:@"SDTableViewCell" owner:nil options:nil] lastObject];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.purchaseBtn addTarget:self action:@selector(purchaseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    cell.model =self.sdArray[indexPath.row];
    [cell.xianmuBtn addTarget:self action:@selector(praiseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        cell.xianmuBtn.tag =3000+indexPath.row;
    [cell.jiduBtn addTarget:self action:@selector(jiduBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        cell.jiduBtn.tag =2000+indexPath.row;
    [cell.henBtn addTarget:self action:@selector(henBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        cell.henBtn.tag =1000+indexPath.row;

}
    return cell;
}
//羡慕
- (void)praiseBtnAct:(UIButton *)button
{
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]) {
        [Tools showAlert:@"您还未登录,请到个人登录!"];
        return;
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag - 3000 inSection:0];
    
    SDTableViewCell *cell = [self.shareHomeTableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kSDPraiseUrl];
    //NSLog(@"%@",cell.model.shaidan_id);
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"shaidan_id":cell.model.shaidan_id,@"pro_type":@"1"};
    [NetworkHandle getInfoWithUrl:url dictionary:dic success:^(NSDictionary *dictionary) {
         //NSLog(@"%@", dictionary);
        if([dictionary[@"code"] isEqualToString:@"200"]) {
            
        cell.xianmuNub.text = [NSString stringWithFormat:@"(%ld)", [cell.model.xianmu_num integerValue] + 1];
        cell.model.xianmu_num = [NSString stringWithFormat:@"%ld",[cell.model.xianmu_num integerValue] + 1];;
          

        }
        else
        {
            [Tools showAlert:dictionary[@"message"]];
            
        }
    }];

}
- (void)jiduBtnAct:(UIButton*)button
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]) {
        [Tools showAlert:@"您还未登录,请到个人登录!"];
        return;
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag - 2000 inSection:0];
    
    SDTableViewCell *cell = [self.shareHomeTableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kSDPraiseUrl];
    //NSLog(@"%@",cell.model.shaidan_id);
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"shaidan_id":cell.model.shaidan_id,@"pro_type":@"2"};
    [NetworkHandle getInfoWithUrl:url dictionary:dic success:^(NSDictionary *dictionary) {
        //NSLog(@"%@", dictionary);
        if([dictionary[@"code"] isEqualToString:@"200"]) {
            
            cell.jiduNub.text = [NSString stringWithFormat:@"(%ld)", [cell.model.jidu_num integerValue] + 1];
            cell.model.jidu_num = [NSString stringWithFormat:@"%ld",[cell.model.jidu_num integerValue] + 1];
            
            
        }
        else
        {
            [Tools showAlert:dictionary[@"message"]];
            
        }
    }];

}
- (void)henBtnAct:(UIButton*)button
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]) {
        [Tools showAlert:@"您还未登录,请到个人登录!"];
        return;
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag - 1000 inSection:0];
    
    SDTableViewCell *cell = [self.shareHomeTableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kSDPraiseUrl];
    //NSLog(@"%@",cell.model.shaidan_id);
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"shaidan_id":cell.model.shaidan_id,@"pro_type":@"3"};
    [NetworkHandle getInfoWithUrl:url dictionary:dic success:^(NSDictionary *dictionary) {
        //NSLog(@"%@", dictionary);
        if([dictionary[@"code"] isEqualToString:@"200"]) {
            
            cell.henNub.text = [NSString stringWithFormat:@"(%ld)", [cell.model.hen_num integerValue] + 1];
            cell.model.hen_num =[NSString stringWithFormat:@"%ld",[cell.model.hen_num integerValue] + 1];;
        }
        else
        {
            [Tools showAlert:dictionary[@"message"]];
            
        }
    }];

}
- (void)purchaseBtnAct:(UIButton *)btn
{
    self.tabBarController.selectedIndex = 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return 165;
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDDetailViewController *sdDetailVC = [[SDDetailViewController alloc]init];
    sdDetailVC.model = self.sdArray[indexPath.row];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:sdDetailVC animated:YES];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
